package com.siloam.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.siloam.module.Navigation;
import com.siloam.service.NavService;
import com.siloam.vo.NavVO;
import com.siloam.vo.TreeNodeVO;

@Controller
@RequestMapping("/web/nav/nav.do")
public class NavController extends BaseController{

	@Resource
	private NavService navService;

	public NavService getNavService() {
		return navService;
	}

	public void setNavService(NavService navService) {
		this.navService = navService;
	}
	
	@RequestMapping(params="method=add")
	@ResponseBody
	public String add(Navigation nav){
		navService.add(nav);
		return SUCCESS;
	}
	
	@RequestMapping(params="method=delSome")
	@ResponseBody
	public String delete(String ids){
		navService.delete(ids);
		return SUCCESS;
	}
	
	@RequestMapping(params="method=navTree")
	@ResponseBody
	public List<TreeNodeVO> buildNavTree(){
		return navService.buildNavTree();
	}
	
	@RequestMapping(params="method=levelNavs")
	@ResponseBody
	public List<NavVO> findByLevel(int level, int id, int type){
		List<NavVO> navs;
		if(level == Navigation.LV_FRIST){
			navs = navService.findFirstLevel(type);
		} else {
			navs = navService.findSecondLevel(id);
		}
		return navs;
	}
	
	@RequestMapping(params="method=initEdit")
	public String initEdit(int id,ModelMap map){
		Navigation nav = navService.load(id);
		map.put("nav", nav);
		return "/web/nav/editNav";
	}
	
	@RequestMapping(params="method=saveEdit")
	@ResponseBody
	public String saveEdit(Navigation nav){
		navService.update(nav);
		return SUCCESS;
	}
}

